<?php
class VHosts {
	public $path = '';
	public $vhosts_path = '';
	public $hosts_path = '';
	private $domains = array();
	private $hosts = '';
	private $vhosts = "";
	private $host_template = '';
	private $vhost_template = '';
	
	function __construct($params = null) 
	{
		if(is_array($params)) {
			$this->path = $params['path'];
			$this->vhosts_path = $params['vhosts_path'];
			$this->hosts_path = $params['hosts_path'];
			$this->host_template = file_get_contents('.' . DS . 'templates' . DS . 'hosts.template');
			$this->vhost_template = file_get_contents('.' . DS . 'templates' . DS . 'vhosts.template');
			$this->hosts = file_get_contents('.' . DS . 'hosts');
			
			$this->hosts .= $this->getHost("localhost");
			$this->vhosts .= $this->getVHost("localhost", $this->path . DS . "localhost" . DS, $this->path . DS . "localhost" . DS . "logs" . DS);
			
			$this->getDomains();
			$this->build();
			$this->write();
		}
	}
	
	private function getDomains()
	{
		$this->domains = array();
	
		$domains = opendir ($this->path);
		
		while ( $domain = readdir ($domains))
		{
			if (( $domain != ".") && ($domain != "..") && is_dir($this->path . DS . $domain . DS . 'httpdocs') && !strstr($domain, ' '))
			{
				$this->domains[$domain] = true;
				
				if(is_dir($this->path . DS . $domain . DS . 'subdomains')) {
					$subdomains = opendir ($this->path . DS . $domain . DS . 'subdomains');
					
					while ( $subdomain = readdir ($subdomains)) {
						if (( $subdomain != ".") && ($subdomain != "..") && is_dir($this->path . DS . $domain . DS . 'subdomains'. DS . $subdomain . DS . 'httpdocs')) {
							if(!is_array($this->domains[$domain])) {
								$this->domains[$domain] = array();
							}
							$this->domains[$domain][] = $subdomain;
						}
					}
					closedir ($subdomains);
				}
			}
		}
		
		closedir ($domains);
	}
	
	private function build()
	{
		foreach($this->domains as $key => $domain) {
			$this->hosts .= $this->getHost("{$key}.loc");
			$this->vhosts .= $this->getVHost($key . '.loc', $this->path . DS . $key . DS . 'httpdocs' . DS, $this->path . DS . $key . DS . 'logs' . DS);
			
			if(is_array($domain)) {
				foreach($domain as $subdomain) {
					$this->hosts .= $this->getHost("{$subdomain}.{$key}.loc");
					$this->vhosts .= $this->getVHost($key . '.loc', $this->path . DS . $subdomain . '.' . $key . DS . 'httpdocs' . DS, $this->path . DS . $subdomain . '.' . $key . DS . 'logs' . DS);
				}
			}
		}
	}
	
	private function getHost($host)
	{
		return str_replace("{host}", $host, $this->host_template) . "\n";
	}
	
	private function getVHost($host, $path, $logs)
	{
		$this->checkLogs($logs);
		
		return str_replace(array("{host}", "{path}", "{logs}"), array($host, $path, $logs), $this->vhost_template) . "\n";
	}
	
	private function write()
	{
		file_put_contents($this->vhosts_path, $this->vhosts);
		file_put_contents($this->hosts_path, $this->hosts);
	}
	
	private function checkLogs($path)
	{
		if(!is_dir($path)) {
			mkdir($path);
		}
		
		if(!file_exists($path . 'php_error_log')) {
			file_put_contents($path . 'php_error_log', '');
			chmod($path . 'php_error_log', 0777);
		}
		
		if(!file_exists($path . 'error_log')) {
			file_put_contents($path . 'error_log', '');
			chmod($path . 'error_log', 0777);
		}
		
		if(!file_exists($path . 'access_log')) {
			file_put_contents($path . 'access_log', '');
			chmod($path . 'access_log', 0777);
		}
	}
}
?>
